﻿using System;
using System.Collections.Generic;
using System.Text;
using Hanhud.Security;

namespace Hanhud.Web.Masters
{
    public class SECUREMaster : HANHUDMaster
    {
        public SECUREMaster()
        {
            this.Load -= new EventHandler(SECUREMaster_Load);
        }

        public override string ClientID
        {
            get
            {
                return "SECUREMaster";
                //return base.ClientID;
            }
        }

        void SECUREMaster_Load(object sender, EventArgs e)
        {
            // get the current page...
            string pageSrc = GetPageSource();

            try
            {
                Hanhud.Web.UI.SitePage currentPage = (Hanhud.Web.UI.SitePage)Page.LoadControl(pageSrc);

                    CurrentPage = currentPage;
                    CurrentPage.Master = this;
                    CurrentPage.Header = Header;
                    CurrentPage.Footer = Footer;
                    CurrentPage.Navigator = Navigator;

                    CPageContext.Current.CurrentPage = CurrentPage;

                    // add the header control before the page rendering...
                    Controls.AddAt(0, Header);
                    Controls.AddAt(1, Navigator);
                    // add the page before the page rendering...
                    Controls.Add(CurrentPage);
                    // add the footer control after the page...
                    Controls.Add(Footer);
                    
                    //// Phat sinh permission exception, thong bao ko du tham quyen de truy cap vao tai nguyen nay
                    //throw new System.UnauthorizedAccessException(Hanhud.Web.Resources.DException.DeniedSelect);
            }
            catch (System.IO.FileNotFoundException)
            {
                // Phat sinh application exception, thong bao tai nguyen truy xuat khong ton tai hoac khong co tham quyen
                throw new ApplicationException(Hanhud.Web.Resources.HANHUD.ResourceNotFound);
            }
        }

        protected override string GetPageSource()
        {
            string pag = string.IsNullOrEmpty(Request.QueryString["pag"]) ? "Welcome" : Request.QueryString["pag"];

            return string.Format("~/Pages/Secure/{0}.ascx", pag);
        }
    }
}
